import { _decorator, Component, Label, Node, Sprite } from 'cc';
import { MapZone } from '../entity/MapZone';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { VillageInformation } from '../world/VillageInformation';
import VillageApi from '../api/VillageApi';
import { EventManager } from '../event/EventManager';
import { EventOper } from '../entity/Buildings';
import { Utils } from '../common/Utils';
import { UserInfo } from '../common/UserInfo';
const { ccclass, property } = _decorator;

@ccclass('OasisItem')
export class OasisItem extends Component {
    @property(Sprite)
    cover:Sprite
    @property(Label)
    coord:Label
    @property(Label)
    desc:Label
    @property(Label)
    loty:Label
    @property(Node)
    giveup:Node
    @property(Node)
    cancel:Node

    private zone:MapZone

    start() {
        this.node.on(Node.EventType.TOUCH_END, this.onClick, this)
        this.cancel.on(Node.EventType.TOUCH_END, this.onCancel, this)
        this.giveup.on(Node.EventType.TOUCH_END, this.onGiveup, this)
    }

    async setData(zone:MapZone){
        this.zone = zone

        this.cover.spriteFrame = await zone.getImg()
        let xy = MapZone.getXyByZoneId(zone.zoneId)
        this.coord.string = `(${xy[0]}|${xy[1]})`

        this.desc.string = zone.desc
        this.loty.string = `忠诚度+${zone.faithful}%`

        if(zone.st){
            this.desc.string = zone.st + ''
            this.loty.node.parent.active = false
            this.giveup.active = false
            this.cancel.active = true
            this.cancel.position = this.giveup.position

            let offsetTime =zone.st - Math.floor(UserInfo.instance.serverTime / 1000) 
            this.countDown()
            this.schedule(this.countDown, 1, offsetTime - 1)
            
        } else{
            this.cancel.active = false
        }
    }

    async onClick(){
        let ui = await UIManager.instance.openUI(PrefabEnum.VillageInformation)
       
        ui.getComponent(VillageInformation).setData(this.zone)
    }

    async onGiveup(){
        await UIManager.instance.showLoading()
        await VillageApi.cancelOasis(this.zone.zoneId)
        UIManager.instance.closeLoading()

        EventManager.instance.emit(EventManager.EventType.RefreshOasisList, 1)
    }

    async onCancel(){
        await UIManager.instance.showLoading()
        await VillageApi.eventOper({
            ac:EventOper.CANCEL,
            eid: this.zone.eventHandle
        })
        UIManager.instance.closeLoading()
        EventManager.instance.emit(EventManager.EventType.RefreshOasisList, 1)
    }

    countDown(){
        let offsetTime =this.zone.st - Math.floor(UserInfo.instance.serverTime / 1000)
        if(offsetTime <=0){
            this.scheduleOnce(() => {
                EventManager.instance.emit(EventManager.EventType.RefreshOasisList, 1)
            }, 2)
            return
        }
        
        this.desc.string = '需时：' + Utils.secondToStr(offsetTime)
        
    }

}


